package com.lframework.starter.security.controller.system;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.lframework.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.mybatis.entity.SysOrg;
import com.lframework.starter.mybatis.resp.PageResult;
import com.lframework.starter.mybatis.service.system.ISysOrgService;
import com.lframework.starter.mybatis.utils.PageResultUtil;
import com.lframework.starter.mybatis.vo.system.org.QuerySysOrgVo;
import com.lframework.starter.web.controller.DefaultBaseController;
import com.lframework.starter.web.resp.InvokeResult;
import com.lframework.starter.web.resp.InvokeResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 组织表 Controller
 *
 * @author laich
 */
@Slf4j
@Api(tags = "组织表")
@Validated
@RestController
@RequestMapping("/system/org")
public class SysOrgController extends DefaultBaseController {

    @Autowired
    private ISysOrgService sysOrgService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@permission.valid('system:org:query')")
    @GetMapping("/query")
    public InvokeResult<PageResult<SysOrg>> query(@Valid QuerySysOrgVo vo) {

        PageResult<SysOrg> pageResult = sysOrgService.query(getPageIndex(vo), getPageSize(vo), vo);

        List<SysOrg> datas = pageResult.getDatas();
        List<SysOrg> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas ;//datas.stream().map(SysOrg::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 查询组织
     */
    @ApiOperation("查询组织")
    @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
    @PreAuthorize("@permission.valid('system:org:query','system:org:add','system:org:modify')")
    @GetMapping
    public InvokeResult<SysOrg> get(@NotNull(message = "ID不能为空！") Integer id) {
        SysOrg data = sysOrgService.findById(id);
        if (data == null) {
            throw new DefaultClientException("用户不存在！");
        }
        return InvokeResultBuilder.success(data);
    }

    /**
     * 根据ID删除
     */
    @ApiOperation("根据ID删除")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @PreAuthorize("@permission.valid('system:org:delete')")
    @DeleteMapping
    public InvokeResult<Void> deleteById(@NotNull(message = "id不能为空！") Integer id) {

        sysOrgService.deleteById(id);

        return InvokeResultBuilder.success();
    }

    /**
     * 新增
     */
    @ApiOperation("新增组织")
    @PreAuthorize("@permission.valid('system:org:add')")
    @PostMapping
    public InvokeResult<Void> create(@Valid QuerySysOrgVo vo) {
        log.info(JSON.toJSONString(vo));
        sysOrgService.saveOrUpdate(vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 修改用户
     */
    @ApiOperation("修改用户")
    @PreAuthorize("@permission.valid('system:user:modify')")
    @PutMapping
    public InvokeResult<Void> update(@Valid QuerySysOrgVo vo) {

        sysOrgService.saveOrUpdate(vo);

        return InvokeResultBuilder.success();
    }
}
